import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';
import { PostModel } from './post.model';

class CreatePostDto {
  @ApiProperty({ title: '帖子标题', default: '帖子标题1' })
  title: string

  @ApiProperty({ title: '帖子内容', default: '帖子内容1' })
  content: string
}

@Controller('posts')
@ApiTags('帖子')
export class PostsController {

  @Get()
  @ApiOperation({ summary: '显示帖子列表' })
  async index() {
    return await PostModel.find()
  }

  @Post()
  @ApiOperation({ summary: '创建帖子' })
  async create(@Body() createPostDto: CreatePostDto) {
    await PostModel.create(createPostDto)
    return {
      success: true
    }
  }

  @Get(':id')
  @ApiOperation({ summary: '帖子详情' })
  async detail(@Param('id') id: string) {
    return await PostModel.findById(id)
  }

  @Put(':id')
  @ApiOperation({ summary: '修改帖子' })
  async update(@Param('id') id: string, @Body() updatePostDto: CreatePostDto) {
    await PostModel.findByIdAndUpdate(id, updatePostDto)
    return {
      success: true
    }
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除帖子' })
  async remove(@Param('id') id: string) {
    await PostModel.findByIdAndDelete(id)
    return {
      success: true
    }
  }
}
